package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.LocalAlbumActivity;
import com.github.postsanf.yinian.activity.LocalAlbumActivity2;
import com.github.postsanf.yinian.activity.LocalAlbumActivity3;
import com.github.postsanf.yinian.activity.MainActivity;
import com.github.postsanf.yinian.bean.YNImageFloder;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.util.List;

/**
 * Created by Administrator on 2016/7/15.
 */
public class LocalAlbumAdapter extends BaseAdapter {
    private Context mContext;
    private List<YNImageFloder> mImageFloders;

    public LocalAlbumAdapter(Context context, List<YNImageFloder> mImageFloders) {
        this.mContext = context;
        this.mImageFloders = mImageFloders;
    }

    public int getCount() {
        return mImageFloders.size();
    }

    public Object getItem(int position) {
        return mImageFloders.get(position);
    }

    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from
                    (mContext).inflate(R.layout.localalnum_item, null, false);
            holder.item = (RelativeLayout) convertView.findViewById(R.id.local_album_dir_item);
            holder.iv = (SimpleDraweeView) convertView.findViewById(R.id.local_album_dir_image);
            holder.dirname = (TextView) convertView.findViewById(R.id.local_album_dir_name);
            holder.dircount = (TextView) convertView.findViewById(R.id.local_album_dir_count);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final YNImageFloder listItems = mImageFloders.get(position);
        Uri uri = Uri.parse("file://" + listItems.getFirstImagePath());
        ImageOptHelper.showFrescoNative(uri, holder.iv, mContext);
        holder.dirname.setText(listItems.getName());
        holder.dircount.setText("(" + listItems.getCount() + ")");
        holder.item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (MainActivity.goodsid) {
                    case 1:
                        mContext.startActivity(new Intent(mContext, LocalAlbumActivity.class).putExtra(LocalAlbumActivity.class.getSimpleName(), listItems));
                        break;
                    case 2:
                        mContext.startActivity(new Intent(mContext, LocalAlbumActivity3.class).putExtra(LocalAlbumActivity3.class.getSimpleName(), listItems));
                        break;
                    case 3:
                        mContext.startActivity(new Intent(mContext, LocalAlbumActivity2.class).putExtra(LocalAlbumActivity2.class.getSimpleName(), listItems));
                        break;
                    case 4:
                        mContext.startActivity(new Intent(mContext, LocalAlbumActivity2.class).putExtra(LocalAlbumActivity2.class.getSimpleName(), listItems));
                        break;
                    case 99:
                        mContext.startActivity(new Intent(mContext, LocalAlbumActivity.class).putExtra(LocalAlbumActivity.class.getSimpleName(), listItems));
                        break;
                    case 101://拍立得选图
                        mContext.startActivity(new Intent(mContext, LocalAlbumActivity.class).putExtra(LocalAlbumActivity.class.getSimpleName(), listItems));
                        break;
                }
            }
        });
        return convertView;
    }

    private class ViewHolder {
        RelativeLayout item;
        SimpleDraweeView iv;
        TextView dirname;
        TextView dircount;
    }


}
